//
// Created by YxMin on 2024/7/5.
//

#include <network.h>
#include <cstdio>
#include "procedure.h"

char PlayerBuffer[128];
bool SendPlayerPositionUpdatePackToServer(int id,int x,int y,int direction,int tick) {
    sprintf(PlayerBuffer, "<([%d%c%08d,%d%03d%03d])>", id,1 | 0x30, tick, direction, x, y);
    return QueueSendDataToServer(PlayerBuffer);
}

//bool SendProjectilePositionUpdatePackToServer(int playerId,int projectileId,int x,int y,int tick) {
//    sprintf(PlayerBuffer, "<([%d%c%08d,%04d%03d%03d])>", playerId, 2 | 0x30, tick, projectileId, x, y);
//    return QueueSendDataToServer(PlayerBuffer);
//}
bool SendPlayerAccumulatePowerPackToServer(int id,int second,int tick) {
    sprintf(PlayerBuffer, "<([%d%c%08d,%1d])>", id, 3 | 0x30, tick, second);
    return QueueSendDataToServer(PlayerBuffer);
}
//每个玩家的炮弹id是独立的
bool SendProjectileShootPackToServer(int id,int projectileId,int level,int x,int y,int direction,int tick) {
    sprintf(PlayerBuffer, "<([%d%c%08d,%04d%03d%03d%1d%1d])>", id, 4 | 0x30, tick, projectileId,x,y,direction,level);
    return QueueSendDataToServer(PlayerBuffer);
}

bool SendPlayerAmmunitionUpdatePackToServer(int id,int ammunition,int tick) {
    sprintf(PlayerBuffer, "<([%d%c%08d,%1d])>", id, 6 | 0x30, tick, ammunition);
    return QueueSendDataToServer(PlayerBuffer);
}

//server

bool BroadcastPlayerPositionUpdatePackToPlayer(int playerId,int tick,int x,int y,int direction,char* buffer) {
    sprintf(buffer, "<([%d%c%08d,%d%03d%03d])>", playerId, 1 | 0x30, tick, direction, x, y);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastPlayerPositionForceUpdatePackToPlayer(int playerId,int tick,int x,int y,int direction,char* buffer) {
    sprintf(buffer, "<([%d%c%08d,%d%03d%03d1])>", playerId, 1 | 0x30, tick, direction, x, y);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastProjectileCrashPackToPlayer(int playerId,int tick,int projectileId,char *buffer) {
    sprintf(buffer, "<([%d%c%08d,%04d])>", playerId, 2 | 0x30, tick, projectileId);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastPlayerAccumulatePowerPackToPlayer(int playerId,int tick,int second,char *buffer) {
    sprintf(buffer, "<([%d%c%08d,%1d])>", playerId, 3 | 0x30, tick,second);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastProjectileShootPackToPlayer(int playerId,int projectileId,int x,int y,int direction,int level,int tick,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%04d%03d%03d%1d%1d])>",playerId, 4 | 0x30,tick,projectileId,x,y,direction,level);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastProjectileDurabilityUpdatePackToPlayer(int playerId,int projectileId,int tick,int durability, char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%04d%1d])>",playerId, 5 | 0x30,tick,projectileId,durability);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastPlayerAmmunitionUpdatePackToPlayer(int playerId,int tick,int ammunition,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%1d])>",playerId, 6 | 0x30,tick,ammunition);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastPlayerHealthUpdatePackToPlayer(int playerId,int tick,int health,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%1d])>",playerId, 7 | 0x30,tick,health);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

//此处的playerId要追溯到发射炮弹的那名玩家
bool BroadcastBlockHealthUpdatePackToPlayer(int playerId,int tick,int col,int row,int health,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%02d%02d%1d])>",playerId, 8 | 0x30,tick,col,row,health);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastPlayerBaseHealthUpdatePackToPlayer(int playerId,int tick,int health,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%1d])>",playerId, 9 | 0x30,tick,health);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastGameOverPackToPlayer(int winnerId,int tick,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%d])>",winnerId, 10 | 0x30,tick,winnerId);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}

bool BroadcastPlayerDisconnectPackToPlayer(int playerId,int tick,char *buffer) {
    sprintf(buffer,"<([%d%c%08d,%d])>",playerId, 11 | 0x30,tick,playerId);
    return BroadcastDataToClientExceptAndKickIfFail(buffer);
}